package com.hmall.item.web;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.item.R.PageBean;
import com.hmall.item.R.PageInfoDTO;
import com.hmall.item.config.MqConfiguration;
import com.hmall.item.pojo.Item;
import com.hmall.item.pojo.ItemDoc;
import com.hmall.item.service.IItemService;

import java.io.IOException;
import java.util.ArrayList;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController
@RequestMapping("/item")
@RequiredArgsConstructor
public class ItemController {

    private final IItemService itemService;

    private final RabbitTemplate rabbitTemplate;

    private final RestHighLevelClient restHighLevelClient;

    @GetMapping("/list")
    public PageBean<Item> list(PageInfoDTO pageInfoDTO) {
        Page<Item> page = new Page<>(pageInfoDTO.getPage(), pageInfoDTO.getSize());
        itemService.selectPage(page, null);

        PageBean<Item> pageBean = new PageBean<>();
        pageBean.setTotal(page.getTotal());
        pageBean.setList(page.getRecords());

        return pageBean;
    }

    @GetMapping("/{id}")
    public Item rowInfo(@PathVariable Long id) {
        return itemService.selectById(id);
    }

    @PostMapping
    public void insert(@RequestBody Item item) {
        itemService.insert(item);
        rabbitTemplate.convertAndSend(MqConfiguration.ITEM_TOPIC, MqConfiguration.ITEM_INSERT, item.getId());
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id) {
        itemService.delete(id);
        rabbitTemplate.convertAndSend(MqConfiguration.ITEM_TOPIC, MqConfiguration.ITEM_DELETE, id);
    }

    @PutMapping
    public void update(@RequestBody Item item) {
        itemService.updateItem(item);
        rabbitTemplate.convertAndSend(MqConfiguration.ITEM_TOPIC, MqConfiguration.ITEM_INSERT, item.getId());
    }

    @PutMapping("/status/{id}/{status}")
    public void updateStatus(@PathVariable Long id,
                             @PathVariable Integer status) {
        Item item = itemService.selectById(id);
        item.setStatus(status);
        itemService.updateItem(item);

        if (status == 2) {
            rabbitTemplate.convertAndSend(MqConfiguration.ITEM_TOPIC, MqConfiguration.ITEM_DELETE, item.getId());
        }

        //todo 重新读入ES
        if (status == 1) {
            try {
                IndexRequest indexRequest = new IndexRequest("item").id(String.valueOf(id));
                ItemDoc itemDoc = new ItemDoc(item);
                indexRequest.source(JSON.toJSONString(itemDoc), XContentType.JSON);
                restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
